# -*- coding: utf-8 -*-
"""
Created on Mon Oct 10 14:47:08 2016

@author: jockeyyan
"""
import pymongo
from pymongo import MongoClient
import jieba

# Open db and collection
def getStores():
    client = MongoClient("172.16.0.249", 27017)
    db = client["test"]
    stores = db.get_collection("stores")
    return(stores)

def insStores(stores):
    stores.insert_one({"name" : "Coffee", "description" : "Very delicious coffee"})
    stores.insert_one({"name" : "Starbucks", "description" : "The most famous coffee bar"})
    stores.insert_one({"name" : "MAAN Coffee", "description" : "装修风格独具一格的咖啡店"})
    stores.insert_one({"name" : "猫小院", "description" : "一群可爱的猫，咖啡味道一般"})
    stores.insert_one({"name" : "蓝杉西餐咖啡", "description" : "空间中等规模，装修风格蓝色家庭风"})
    stores.insert_one({"name" : "COFFEE HOLIC 精品咖啡", "description" : "开在国贸商圈的精品咖啡馆 其实一开始看门脸还有点小失望 但是进去尝了咖啡还是觉得值回票价的"})
    stores.insert_one({"name" : "咖啡茶自助餐厅(香格里拉饭店)", "description" : "今天来了好多爷爷和奶奶，挤爆了龙虾台和大闸蟹台。一个奶奶把大闸蟹摆成了必胜客自助沙拉的塔型，我觉得2016感动中国人物她是跑不了了。"})
    stores.insert_one({"name" : "北平咖啡(南锣鼓巷店)", "description" : "进门绿意葱茏，各种温馨，二楼还有露台，又美又浪漫，本来想坐下，结果被小飞虫和团队聚餐的热闹场面一起打败了"})

def addTextIndex(stores):
    stopwords = [' ', ',', '.', '/', '<', '>', '?', ';', '\'',
                 ':', '"', '[', ']', '\\', '`', '~', '!', '@',
                 '#', '$', '%', '^', '&', '*', '(', ')', '-', 
                 '_', '+', '=']
    for store in stores.find():
        #print store["name"]
        # 搜索引擎模式
        token1 = jieba.lcut_for_search(store["name"])
        token2 = jieba.lcut_for_search(store["description"])
        token1.extend(token2)
        # 为了查询方便字母统一转为小写
        for i, tok in enumerate(token1):
            token1[i] = tok.lower()
        # 去除空格和标点符号等
        token = list(set(token1) - set(stopwords))    
        #store["txtidx"] = tokens
        result = stores.update_one(
            {"_id": store["_id"]},
            {
                "$set": {
                    "txtidx": token
                },
                "$currentDate": {"lastModified": True}
            }
        )
    stores.create_index([("txtidx", pymongo.ASCENDING)])
    
def fullTextSearch(stores, keys):
    # 首先按空格区分“或”查询关系

    # 查询的关键字需要分词且转为小写
    token = jieba.lcut_for_search(keys)
    for i, tok in enumerate(token):
        token[i] = tok.lower()
    #stores.find({txtidx : {$all : ["Coffee"]}})
    rs = stores.find({"txtidx" : {"$all" : token}})
    return(rs)    
    
if __name__ == '__main__':
    stores = getStores()
    
#    # 初始化并建立全文索引
#    insStores(stores)
#    addTextIndex(stores)
    
    # 通过全文搜索
    #keyword = "COFFEE"
    #keyword = "猫"
    #keyword = "咖啡"
    keyword = "咖啡Maan"
    rs = fullTextSearch(stores, keyword)
    for doc in rs:
        print(str(doc["_id"]) + " : " + doc["name"] + " : " + doc["description"])
    